package com.myproject.zhujiemian;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import my.project.kebiao.kebiaoCurriculum;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.CycleInterpolator;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.itheima.tencentqq52.R;
import com.myproject.biji.bijiMainActivity;
import com.myproject.database.MYdatabase;
import com.myproject.database.MyAdapter;
import com.myproject.database.Task;
import com.myproject.myutile.Utils;
import com.myproject.zidingyikongjian.DragLayout;
import com.myproject.zidingyikongjian.MyLinearLayout;
import com.myproject.zidingyikongjian.DragLayout.OnDragStatusChangeListener;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.view.ViewHelper;

public class MainActivity extends Activity {
//	private Button addtaskbt, zhanghubt, jishibenbt, kebiaobt, jianchibt,
//			tongjibt;
	private ListView tasklistlist;
	private ImageView headiv;
	private MyLinearLayout zhubujumy;
	private Cursor cursor;
	private SQLiteDatabase dbReader;
	private MYdatabase taskDB;

	private MyAdapter adapter;
//	List<Task> taskList;
	private DragLayout zuobujumy;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		
//		taskList = new ArrayList<Task>();
		
		taskDB = new MYdatabase(this);
		dbReader = taskDB.getReadableDatabase();
		
		zuobujumy = (DragLayout) findViewById(R.id.my_zuobuju);

		// final ListView mLeftList = (ListView) findViewById(R.id.lv_left);
		tasklistlist = (ListView) findViewById(R.id.list_tasklist);
		headiv = (ImageView) findViewById(R.id.iv_head);
		zhubujumy = (MyLinearLayout) findViewById(R.id.my_zhubuju);

		// 查找Draglayout, 设置监听
		DragLayout mDragLayout = (DragLayout) findViewById(R.id.my_zuobuju);

		// 设置引用
		zhubujumy.setDraglayout(mDragLayout);
		
		tasklistlist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				cursor.moveToPosition(position);
				Intent i = new Intent(MainActivity.this ,ToDoTask.class);
//				Intent i2 = new Intent(MainActivity.this, TaskJishi.class);
//				i2.putExtra(MYdatabase.ID,
//						cursor.getInt(cursor.getColumnIndex(MYdatabase.ID)));
				i.putExtra(MYdatabase.ID,
						cursor.getInt(cursor.getColumnIndex(MYdatabase.ID)));
				i.putExtra(MYdatabase.CONTENT, cursor.getString(cursor
						.getColumnIndex(MYdatabase.CONTENT)));
				i.putExtra(MYdatabase.TIME,
						cursor.getString(cursor.getColumnIndex(MYdatabase.TIME)));
				
				startActivity(i);
				
//				Intent i2 = new Intent(MainActivity.this, TaskJishi.class);
//				i2.putExtra(MYdatabase.ID,
//						cursor.getInt(cursor.getColumnIndex(MYdatabase.ID)));
//				startActivity(i2);
			}
		});

		mDragLayout.setDragStatusListener(new OnDragStatusChangeListener() {

			@Override
			public void onOpen() {
			}

			@Override
			public void onDraging(float percent) {
			}

			@Override
			public void onClose() {
				
			}
		});
		
		
	
	}
	
	public void ToZuobuju(View v){
		zuobujumy.open(true);
	    }


	// 跳转到增加任务页面
	public void ToAddTask(View view) {
		Intent intent = new Intent(this, AddTask.class);
		intent.setAction("com.myproject.AddTask");
		startActivity(intent);

	}

	// 跳转到坚持页面
	public void ToJianchiPage(View view) {
		Intent intent = new Intent(this, JianchiPage.class);
		intent.setAction("com.myproject.JianchiPage");
		startActivity(intent);
	}

	// 跳转到统计页面
	public void ToTongjiPage(View view) {
		Intent intent = new Intent(this, TongjiPage.class);
		intent.setAction("com.myproject.TongjiPage");
		
		
//		intent.putExtra(MYdatabase.ID, getIntent().getIntExtra(MYdatabase.ID, 0));
		startActivity(intent);
	}

	// 跳转到账户页面
	public void ToZhanghuPage(View view) {
		Intent intent = new Intent(this, ZhanghuPage.class);
		intent.setAction("com.myproject.ZhanghuPage");
		startActivity(intent);
	}

	// 跳转到记事本页面
	public void JishibenPage(View view) {
		Intent intent = new Intent(this, bijiMainActivity.class);
		intent.setAction("com.myproject.bijiMainActivity");
		startActivity(intent);
	}
	// 跳转到课表页面
	public void ToKebiaoPage(View view) {
		Intent intent = new Intent(this, kebiaoCurriculum.class);
		intent.setAction("com.myproject.kebiaoCurriculum");
		startActivity(intent);
	}
	
	
	@Override
	protected void onResume() {
		super.onResume();
		selectDB();
	}
	
	public void selectDB() {
		cursor = dbReader.query(MYdatabase.TABLE_NAME, null, null, null, null,
				null, null);
		adapter = new MyAdapter(this, cursor);
		tasklistlist.setAdapter(adapter);
	}
	
//	public void selectDB2() {
//		cursor = dbReader.query(MYdatabase.TABLE_NAME, null, null, null, null,
//				null, null);
//		while(cursor.moveToNext()){
//	          String _id = cursor.getString(0);
//	          String content = cursor.getString(1);
//	          String time = cursor.getString(2);
//	          Task p = new Task(_id, content, time);
//	           taskList.add(p);
//	        }
//	       
//		adapter = new MyAdapter(this, cursor);
//		tasklistlist.setAdapter(adapter);
//	}
	
	
	
}
